#pragma once
#include <iostream>
#include <string>
#include <jsoncpp/json/json.h>
#include <memory>

static const std::string seq = "\r\n";
// 添加报文
// 格式：len\r\njsonstr\r\n
std::string Encode(std::string &jsonstr)
{
    int len = jsonstr.size();
    std::string lenstr = std::to_string(len);
    return lenstr + seq + jsonstr + seq;
}

// 检测报文
//  "le
//  "len"
//  "len"\r\n
//  "len"\r\n"{json}"\r\n (]
//  "len"\r\n"{j
//  "len"\r\n"{json}"\r\n"len"\r\n"{
//  "len"\r\n"{json}"\r\n
//  "len"\r\n"{json}"\r\n"len"\r\n"{json}"\r\n"len"\r\n"{json}"\r\n"len"\r\n"{json}"\r
std::string Decode(std::string &packagestream)
{
    auto pos = packagestream.find(seq);
    // 说明报文没有\r\n,报文不完整
    if (pos == std::string::npos)
        return std::string();
    // 计算报文的总长度
    std::string lenstr = packagestream.substr(0, pos);
    int len = stoi(lenstr);
    int total = lenstr.size() + len + 2 * seq.size();
    if (packagestream.size() < total)
        return std::string();
    // 到这说明收到的报文流一定存在一个完整的报文
    std::string jsonstr = packagestream.substr(pos + seq.size(), len);
    packagestream.erase(0, total);
    return jsonstr;
}

class Request
{
public:
    Request()
    {
    }
    Request(int x, int y, char oper) : _x(x), _y(y), _oper(oper)
    {
    }
    bool Serialize(std::string *out)
    {
        Json::Value root;
        root["x"] = _x;
        root["y"] = _y;
        root["oper"] = _oper;
        Json::FastWriter writer;
        std::string s = writer.write(root);
        *out = s;
        return true;
    }
    bool DeSerialize(std::string &str)
    {
        Json::Value root;
        Json::Reader read;
        bool ret = read.parse(str, root);
        _x = root["x"].asInt();
        _y = root["y"].asInt();
        _oper = root["oper"].asInt();
        return true;
    }
    int get_x()
    {
        return _x;
    }
    int get_y()
    {
        return _y;
    }
    char get_oper()
    {
        return _oper;
    }
    void set_value(int x,int y,char oper)
    {
        _x=x;
        _y=y;
        _oper=oper;
    }
private:
    int _x;
    int _y;
    char _oper;
};

class Response
{
public:
    Response() : _result(0), _code(0), _desc("success")
    {
    }
    bool Serialize(std::string *out)
    {
        Json::Value root;
        root["result"] = _result;
        root["code"] = _code;
        root["desc"] = _desc;
        Json::FastWriter writer;
        std::string s = writer.write(root);
        *out = s;
        return true;
    } 
    bool DeSerialize(std::string &str)
    {
        Json::Value root;
        Json::Reader read;
        bool ret = read.parse(str, root);
        _result = root["result"].asInt();
        _code = root["code"].asInt();
        _desc = root["oper"].asString();
        return true;
    }
    int get_result()
    {
        return _result;
    }
    int get_code()
    {
        return _code;
    }
    std::string get_desc()
    {
        return _desc;
    }
    void set_result(int result)
    {
        _result=result;
    }
     void set_code(int code)
    {
        _code=code;
    }
     void set_desc(std::string desc)
    {
        _desc=desc;
    }
    void PrintResult()
    {
        std::cout << "result: " << _result << ", code: " << _code << ", desc: " << _desc << std::endl; 
    }
private:
    int _result;
    int _code;         // 返回码
    std::string _desc; // 返回码描述
};

class Factory
{
public:
    static std::shared_ptr<Request> BuildRequestDefault()
    {
        return std::make_shared<Request>();
    }
    static std::shared_ptr<Response> BuildResponseDefault()
    {
        return std::make_shared<Response>();
    }
};